package com.tyj.movie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tyj.movie.entity.Movie;
import com.tyj.movie.entity.MovieType;
import com.tyj.movie.service.MovieTypeService;
import com.tyj.movie.to.MovieTo;
import com.tyj.movie.entity.MovieArea;
import com.tyj.movie.service.MovieAreaService;
import com.tyj.movie.service.MoviePictureSetService;
import com.tyj.movie.service.MovieServer;
import com.tyj.movie.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tyj
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/movie")
public class MovieController {

    @Autowired
    MovieServer movieService;

    @Autowired
    MovieAreaService movieAreaService;

    @Autowired
    MoviePictureSetService moviePictureSetService;

    @Autowired
    MovieTypeService movieTypeService;

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("/movieAddOrEditor")
    public ModelAndView toMovieAddOrEditorPage(){
        ModelAndView modelAndView=new ModelAndView();
        List<MovieArea> movieAreas = movieAreaService.list(new QueryWrapper<MovieArea>().orderByAsc("sort"));
        List<MovieType> movieTypes = movieTypeService.list(new QueryWrapper<MovieType>().orderByAsc("sort"));
        modelAndView.setViewName("movieAddOrEditor");
        modelAndView.addObject("movieAreas",movieAreas);
        modelAndView.addObject("movieTypes",movieTypes);
        return modelAndView;
    }

    /**
     * 添加电影
     * @return
     */
    @PostMapping("/add")
    public R addMovie(@RequestBody MovieTo movieTo){
        boolean b = false;
        try {
            b = movieService.addMovie(movieTo);
            if (b){
                return R.ok();
            }else {
                return R.error(1001,"添加失败 服务内部错误 请稍后重试");
            }
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

            return R.error(1001,"添加失败 服务内部错误 请稍后重试");


    }

    /**
     *分页查询电影
     */
    @GetMapping("/moviePagingQuery/{pageNum}")
    public ModelAndView moviePagingQuery(@PathVariable("pageNum") Long pageNum,String movieName){
        ModelAndView modelAndView=new ModelAndView();
        IPage<Movie> page=movieService.moviePagingQuery(pageNum,movieName);
        modelAndView.addObject("movieName",movieName);
        modelAndView.addObject("page",page);
        modelAndView.setViewName("/movieTables");
        return modelAndView;
    }

    /**
     * 电影启用
     */
    @GetMapping("/enable/{id}")
    public ModelAndView enable(@PathVariable("id") Long id){
        movieService.enable(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("redirect:/movie/moviePagingQuery/1");
        return modelAndView;
    }

    /**
     * 电影禁用
     */
    @GetMapping("/disable/{id}")
    public ModelAndView disable(@PathVariable("id") Long id){
        movieService.disable(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("redirect:/movie/moviePagingQuery/1");
        return modelAndView;
    }
}

